#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>


void worker(int cnt)
{
    printf("I am a child, my pid : %d, cnt : %d\n",getpid(), cnt);
    sleep(1);
}

int main()
{
    pid_t id = fork();
    if(id == 0)
    {
       // child
       int cnt = 10;
       while(cnt)
       {
           worker(cnt);
           sleep(2);
           cnt--;
       }

       exit(0);
    }   
    else 
    {
        // father
        int status = 0;

        pid_t rid = waitpid(id, &status, 0);

        if (rid > 0)
        {
            printf("child quit success, exit code: %d, exit signal: %d\n", (status>>8)&0xFF, status&0x7F);
            //break;
        }
        else 
        {
            printf("wait fail\n");
            //break;
        }    
    }       
    return 0;
}
